$(function(){
	
	blogType.regAct();
	
});

var blogType = {
		regAct : function() {
			
		},
		
		fn : {
			clearPreparedData : function () {
				$("#addDisplayOrder").val("");
				$("#addName").val("");
			},
			
			add : function(o) {
				var $tr = $(o).parent().parent();
				var displayOrder = $tr.find("[name=displayOrder]").val();
				var name = $tr.find("[name=name]").val();
				if (null==name || name=="") {
					alert("类型不能为空");
					return;
				}
				$.post("admin/blogTypeAction!addBlogType.action", {
					"blogTypeEntity.name":name,
					"blogTypeEntity.displayOrder":displayOrder
				}, function(data){
					if (data.success) {
						$tr.before('<tr><td height="34" align="center" valign="middle"><s:hidden name="id" value="'+data.attr+'"/>'
								+ '<input name="displayOrder" type="text" size="4" value="'+displayOrder+'"></td>'
								+ '<td align="left" valign="middle">'
						        + '<input maxlength="15" size="30" name="name" type="text" value="'+name+'"></td>'
						        +  '<td align="center" valign="middle">'
						        +  '<a href="javascript:void(0);" onclick="blogType.fn.add();">保存</a>&nbsp;&nbsp;'
						        + '<a href="javascript:void(0);" onclick="blogType.fn.del(\''+data.attr+'\');">删除</a></td></tr>');
					}
					//清空添加栏数据
					blogType.fn.clearPreparedData();
					alert(data.msg);
				});
				// 必须是数字 if (displayOrder)
			},
			
			del : function(o) {
				var $tr = $(o).parent().parent();
				var id = $tr.find("[name=id]").val();
				if (!id) {
					alert("数据已过时！点击确定刷新页面");
					window.location.reload();
				}
				
				$.post("admin/blogTypeAction!delBlogType.action", {
					"blogTypeEntity.id":id,
				}, function(data){
					if (data.success) {
						$tr.remove();
					}
					alert(data.msg);
				});
			}
		}
};